package com.zzstm.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzstm.commonUtils.R;
import com.zzstm.eduservice.entity.EduTeacher;
import com.zzstm.eduservice.entity.vo.TeacherQuery;
import com.zzstm.eduservice.service.EduTeacherService;
import com.zzstm.servicebase.ex.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-10-24
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R findAll(){
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }

    //讲师逻辑删除
    @ApiOperation(value = "讲师逻辑删除")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id",value = "讲师id",required = true)
                                @PathVariable String id){
        boolean flag = teacherService.removeById(id);
        return flag? R.ok() : R.error();
    }

    //分页查询
    @GetMapping("/pageTeacher/{pageNum}/{pageSize}")
    public R pageTeacher(@PathVariable Long pageNum,@PathVariable Long pageSize){
        try {
            int i = 1/0;
        }catch (Exception e){
            throw new GuliException(20001,"自定义异常");
        }
        Page<EduTeacher> teacherPage = new Page<>(pageNum,pageSize);
        teacherService.page(teacherPage,null);
        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    //条件查询带分页
    @PostMapping("pageTeacherCondition/{pageNum}/{pageSize}")
    public R pageTeacherCondition(@PathVariable Long pageNum, @PathVariable Long pageSize,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> page = new Page<>(pageNum,pageSize);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        wrapper.orderByDesc("gmt_create");
        teacherService.page(page,wrapper);
        Map<String,Object> map = new HashMap<>();
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        map.put("total",total);
        map.put("records",records);
        return R.ok().data(map);
    }

    //添加讲师
    @PostMapping("/add")
    public R add(@RequestBody EduTeacher teacher){
        return teacherService.save(teacher)? R.ok() : R.error();
    }

    //根据id查询
    @GetMapping("/getOne/{id}")
    public R getOne(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return teacher != null ? R.ok().data("teacher",teacher) : R.error();
    }

    //修改讲师信息
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher teacher){
        boolean flag = teacherService.updateById(teacher);
        return flag ? R.ok() : R.error();
    }

}

